//
//  LQL_photoMainViewController.m
//  Drops
//
//  Created by dllo on 16/5/23.
//  Copyright © 2016年 Drops.er. All rights reserved.
//

#import "LQL_photoMainViewController.h"
#import "LQL_filterViewController.h"
#import "LQL_editorViewController.h"
#import "ClippingViewController.h"
#import <BmobSDK/Bmob.h>
#import "MBProgressHUD.h"
#define SCREENWIDTH [UIScreen mainScreen].bounds.size.width
#define SCREENHEIGHT [UIScreen mainScreen].bounds.size.height
@interface LQL_photoMainViewController ()
@property (nonatomic, retain) UIImageView *imageView;
@property (nonatomic, retain) UIView *menuView;
@property (nonatomic, retain) UIView *saveAndUploadView;
@property (nonatomic, retain) UIButton *filterButton;
@property (nonatomic, retain) UIButton *editorButton;
@property (nonatomic, retain) UIButton *clippingButton;
@property (nonatomic, retain) UIButton *saveButton;
@property (nonatomic, retain) UIButton *uplodaButton;
@property (nonatomic, retain) UIButton *rightButton;
@property (nonatomic, retain) LQL_filterViewController *filterVC;
@property (nonatomic, retain) LQL_editorViewController *editorVC;
@property (nonatomic, retain) ClippingViewController *clippingVC;
@property (nonatomic, strong) MBProgressHUD *hud;
@property (nonatomic, assign) BOOL temp;
@end

@implementation LQL_photoMainViewController
- (void)viewWillAppear:(BOOL)animated{
    self.filterVC = [[LQL_filterViewController alloc] init];
    self.editorVC = [[LQL_editorViewController alloc] init];
    self.clippingVC = [[ClippingViewController alloc]init];
//    __weak typeof(self) weakSelf = self;
    void (^filterBlock)(UIImage *) = ^( UIImage *image){
        self.imageView.image = image;
    };
    _filterVC.filterBlock = filterBlock;
    
    void (^editorBlock)(UIImage *) = ^( UIImage *image){
        self.imageView.image = image;
    };
    _editorVC.editorBlock = editorBlock;
   
    void (^clippingBlock)(UIImage *) = ^( UIImage *image){
        self.imageView.image = image;
    };
    _clippingVC.clippingBlock = clippingBlock;
    

}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor blackColor];
    //    设置模糊效果
    UIBlurEffect *effect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
    UIVisualEffectView *effectView = [[UIVisualEffectView alloc] initWithEffect:effect];
    effectView.frame = self.view.frame;
    //    模糊效果的透明度不能更改
        effectView.alpha = 0.8;
    [self.view addSubview:effectView];
    
    self.imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 10, SCREENWIDTH, SCREENHEIGHT - 49 - 64 - 20)];
    self.imageView.backgroundColor = [UIColor clearColor];
    self.imageView.contentMode = UIViewContentModeScaleAspectFit;
    self.imageView.image = _picture;
    [self.view addSubview:_imageView];
    self.imageView.userInteractionEnabled = YES;
    
    self.rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.rightButton.frame = CGRectMake(0, 0, 40, 30);
    [self.rightButton setImage:[UIImage imageNamed:@"下拉.png"] forState:0];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:self.rightButton];
    [self.rightButton addTarget:self action:@selector(rightButtonAction) forControlEvents:UIControlEventTouchUpInside];

    UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    backButton.frame = CGRectMake(0, 0, 40, 30);
    [backButton setImage:[UIImage imageNamed:@"back.png"] forState:0];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    [backButton addTarget:self action:@selector(backAction:) forControlEvents:UIControlEventTouchUpInside];

    self.menuView = [[UIView alloc] initWithFrame:CGRectMake(0, SCREENHEIGHT - 64 - 49, SCREENWIDTH, 49)];
//    self.menuView.backgroundColor = [UIColor colorWithRed:236 / 255.0 green:73 / 255.0 blue:63 / 255.0 alpha:1];
    self.menuView.backgroundColor = [UIColor colorWithRed:1 / 255.0 green:175 / 255.0 blue:240 / 255.0 alpha:1];
    [self.view addSubview:self.menuView];
    
//    创建滤镜和编辑两个button
    self.filterButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.filterButton.frame = CGRectMake(SCREENWIDTH / 8, self.menuView.frame.size.height / 4, SCREENWIDTH / 4, self.menuView.frame.size.height / 2);
    [self.menuView addSubview:self.filterButton];
    self.filterButton.backgroundColor = [UIColor clearColor];
    self.filterButton.tintColor = [UIColor blackColor];
    [self.filterButton setTitle:@"滤镜" forState:UIControlStateNormal];
    [self.filterButton addTarget:self action:@selector(clickFilterButton) forControlEvents:UIControlEventTouchUpInside];
    
    self.clippingButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.clippingButton.frame = CGRectMake(SCREENWIDTH / 2 - (SCREENWIDTH / 8), self.menuView.frame.size.height / 4, SCREENWIDTH / 4, self.menuView.frame.size.height / 2);
    [self.menuView addSubview:self.clippingButton];
    self.clippingButton.backgroundColor = [UIColor clearColor];
    self.clippingButton.tintColor = [UIColor blackColor];
    [self.clippingButton setTitle:@"裁剪" forState:UIControlStateNormal];
    [self.clippingButton addTarget:self action:@selector(clickClippingButton:) forControlEvents:UIControlEventTouchUpInside];
    
    self.editorButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.editorButton.frame = CGRectMake(SCREENWIDTH / 8 * 5, self.menuView.frame.size.height / 4, SCREENWIDTH / 4, self.menuView.frame.size.height / 2);
    [self.menuView addSubview:self.editorButton];
    self.editorButton.backgroundColor = [UIColor clearColor];
    self.editorButton.tintColor = [UIColor blackColor];
    [self.editorButton setTitle:@"调整" forState:UIControlStateNormal];
    [self.editorButton addTarget:self action:@selector(clickEditorButton) forControlEvents:UIControlEventTouchUpInside];
    
    [self createSaveAndUploadView];
}
- (void)clickFilterButton{
    [self.navigationController pushViewController:self.filterVC animated:YES];
    self.picture = self.imageView.image;
    self.filterVC.inImage = self.picture;
    NSLog(@"%@", self.filterVC.inImage);
}
- (void)clickEditorButton{
    [self.navigationController pushViewController:self.editorVC animated:YES];
    self.picture = self.imageView.image;
    self.editorVC.inImage = self.picture;
}
-(void)clickClippingButton:(UIButton *)button{
//    self.clippingVC = [[ClippingViewController alloc]init];
    [self.navigationController pushViewController:self.clippingVC animated:YES];
    self.picture = self.imageView.image;
    self.clippingVC.editImage = self.picture;
}



//- (void)tapAction:(UITapGestureRecognizer *)tap {
//    NSLog(@"counttttttt:%ld", self.count);
//    self.count+=1;
//    NSInteger times_count = self.count % 2;
//    if (times_count == 0) {
//        
//        [self.navigationController setNavigationBarHidden:YES animated:YES];
//        self.menuView.hidden = YES;
//    } else {
//        
//        [self.navigationController setNavigationBarHidden:NO animated:YES];
//        self.menuView.hidden = NO;
//    }
//}

- (void)backAction:(UIButton *)button {
    UIImagePickerController *imagePicker = (UIImagePickerController *)self.navigationController;
    if (self.flag == NO) {
        [imagePicker dismissViewControllerAnimated:YES completion:nil];
    } else {
        [self.navigationController popViewControllerAnimated:YES];
    }
}
- (void)createSaveAndUploadView{
    /////////********
    self.saveAndUploadView = [[UIView alloc] initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width - 55.5, -80, 40, 80)];
    self.saveAndUploadView.backgroundColor = [UIColor clearColor];
    //创建保存到本地的button
    self.saveButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.saveButton.frame = CGRectMake(0, 0, 40, 40);
    [self.saveButton setImage:[UIImage imageNamed:@"保存.png"] forState:UIControlStateNormal];
    [self.saveAndUploadView addSubview:self.saveButton];
    [self.saveButton addTarget:self action:@selector(clickRightButton:) forControlEvents:UIControlEventTouchUpInside];
    //创建上传到云端的button
    self.uplodaButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.uplodaButton.frame = CGRectMake(0, 40, 40, 40);
    [self.uplodaButton setImage:[UIImage imageNamed:@"上传.png"] forState:UIControlStateNormal];
    [self.saveAndUploadView addSubview:self.uplodaButton];
    [self.uplodaButton addTarget:self action:@selector(clickRightButton:) forControlEvents:UIControlEventTouchUpInside];
}
- (void)rightButtonAction{
    if (!self.temp) {
        [self.view addSubview:self.saveAndUploadView];
        [UIView animateWithDuration:0.5 animations:^{
            /////****************
            self.saveAndUploadView.frame = CGRectMake([UIScreen mainScreen].bounds.size.width - 55.5, 0, 40, 80);
            [self.rightButton setImage:[UIImage imageNamed:@"上拉.png"] forState:UIControlStateNormal];
        }];
        self.temp = !self.temp;
    } else {
        [UIView animateWithDuration:0.5 animations:^{
            self.saveAndUploadView.frame = CGRectMake([UIScreen mainScreen].bounds.size.width - 60, -80, 40, 80);
            [self.rightButton setImage:[UIImage imageNamed:@"下拉.png"] forState:UIControlStateNormal];
        }];
        self.temp = !self.temp;
    }
}
- (void)clickRightButton:(UIButton *)button{
    if (button == self.saveButton) {
        //保存照片到相册
        UIImageWriteToSavedPhotosAlbum(self.imageView.image, nil, nil, nil);
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"保存成功" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *sureAction = [UIAlertAction actionWithTitle:@"好的" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            [self dismissViewControllerAnimated:YES completion:nil];
        }];
        [alert addAction:sureAction];
        [self presentViewController:alert animated:YES completion:^{
        }];
    } else {
        //保存照片到相册
        UIImageWriteToSavedPhotosAlbum(self.imageView.image, nil, nil, nil);
//        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"保存成功" preferredStyle:UIAlertControllerStyleAlert];
//        UIAlertAction *sureAction = [UIAlertAction actionWithTitle:@"好的" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
//            [self dismissViewControllerAnimated:YES completion:nil];
//        }];
//        [alert addAction:sureAction];
//        [self presentViewController:alert animated:YES completion:^{
//        }];
        
        //在这写上传..............................
        [self saveImage:self.imageView.image withName:@"upload.png"];
        NSString *filePath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, 1, YES) lastObject]stringByAppendingPathComponent:@"upload.png"];
         BmobObject *images = [BmobObject objectWithClassName:@"image"];
         BmobFile *bmobFile = [[BmobFile alloc]initWithFilePath:filePath];
        [images saveInBackgroundWithResultBlock:^(BOOL isSuccessful, NSError *error) {
            self.hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
            [bmobFile saveInBackground:^(BOOL isSuccessful, NSError *error) {
                if (isSuccessful) {
                    [images setObject:bmobFile forKey:@"image"];
                    [images updateInBackgroundWithResultBlock:^(BOOL isSuccessful, NSError *error) {
                        if (isSuccessful) {
                            [self.hud hide:YES afterDelay:0];
                            UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"上传成功" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
                            [alert show];
                        } else {
                            UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"图片上传失败" delegate:self cancelButtonTitle:@"重新输入" otherButtonTitles:nil, nil];
                            [alert show];
                        }
                        
                    }];
                } else {
                    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"图片上传失败" delegate:self cancelButtonTitle:@"重新输入" otherButtonTitles:nil, nil];
                    [alert show];
                }
                
            }];
            
        }];
    }
}
-(void)saveImage:(UIImage *)currentImage withName:(NSString *)imageName{
    NSData *imageData = UIImageJPEGRepresentation(currentImage, 1);
    NSString *fullPath = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"]stringByAppendingPathComponent:imageName];
    [imageData writeToFile:fullPath atomically:NO];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
